﻿using System;
using System.Collections.Generic;
using System.Globalization;

namespace Chevron.OrderDetails.Models
{
    internal static class Utilities
    {
        internal const char ALA_FIELDS_DELIMETER = '\t';

        internal static List<string> AlaSplitFields(string data)
        {
            return AlaSplitFields(data, ALA_FIELDS_DELIMETER);
        }

        internal static List<string> AlaSplitFields(string data, char delimeter)
        {
            var result = new List<string>();
            if (data.IndexOf(delimeter) == -1)
                result.Add(data);
            else
            {
                string[] parts = data.Split(new[] {delimeter});
                result.AddRange(parts);
            }
            return result;
        }

        internal static string ExtractAlaCode(string line)
        {
            string[] parts = line.Trim().Split(new[] {ALA_FIELDS_DELIMETER});
            return parts.Length > 0 ? parts[0] : string.Empty;
        }

        internal static DateTime ParseJsonDateTime(string datePart, string timePart)
        {
            string dtstr = string.Format(@"{0} {1}", datePart, timePart);
            return DateTime.ParseExact(dtstr, @"MM/dd/yyyy HH:mm", CultureInfo.InvariantCulture);
        }

        internal static int ToInt(this string input)
        {
            return Int32.Parse(input);
            //int i;
            //return Int32.TryParse(input, out i) ? i : -1;
        }

        internal static decimal ToDecimal(this string input)
        {
            decimal ret;
            Decimal.TryParse(input, out ret);
            return ret;
        }

        internal static ReferalType ToReferalType(this string input)
        {
            switch (input.ToUpperInvariant())
            {
                case @"F":
                    return ReferalType.Flat;
                case @"%":
                    return ReferalType.Percent;
                default:
                    return ReferalType.None;
            }
        }
    }
}